package com.ruoyi.web.controller.interview;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.HuaWeiSISUtils;
import com.ruoyi.common.utils.QINiuUtils;
import com.ruoyi.interview.domain.InterviewAnswer;
import com.ruoyi.interview.service.IInterviewAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Map;

/**
 * 面试回答接口
 * @author huangjie
 * @date 2025/11/25
 */
@RestController
@RequestMapping("/interview/answer")
public class InterviewAnswerController {

    @Autowired
    private IInterviewAnswerService iInterviewAnswerService;

    /**
     * 测试微信小程序录音文件上传
     * @param file
     * @param questionId
     * @return
     * @throws IOException
     */
    @PostMapping("/testUploadRecord")
    public AjaxResult testUploadRecord(@RequestParam("file") MultipartFile file, Long questionId, OutputStream outputStream)throws IOException{
        System.out.println(questionId);
        String filename = file.getOriginalFilename();
        File target = new File("D:/files/" + filename);
        FileOutputStream fileOutputStream = new FileOutputStream(target);
        InputStream inputStream = file.getInputStream();
//        IOUtils.copy(inputStream, fileOutputStream);
        byte[] bytes = new byte[1024];
        int len;
        while ((len = inputStream.read()) != -1) {
            fileOutputStream.write(bytes, 0, len);
        }
        outputStream.flush();
        outputStream.close();
        return AjaxResult.success();

    }


    /**
     * 提交面试录音
     * @param file 上传的文件
     * @param questionId  问题的id
     * @return
     * @throws IOException
     */
    @PostMapping("/handleAnswer")
    public AjaxResult handleAnswer(MultipartFile file,Long questionId) throws IOException {
        //1.解析接收的文件
            //获取文件名
            String filename = file.getOriginalFilename();

            //上传到七牛云 获取七牛云返回的数据
            QINiuUtils.upload(file.getInputStream(),filename);
            //把信息存储到数据库
            InterviewAnswer interviewAnswer = new InterviewAnswer();
            //信息赋值
            interviewAnswer.setQuestionId(questionId);
            final String fileUrl = Constants.QINIU_URL_PREFIX+filename;
            //设置地址
            interviewAnswer.setAudioUrl(fileUrl);
            //执行新增
            iInterviewAnswerService.insertInterviewAnswer(interviewAnswer);

            //获取要修改的id
            final Long answerId = interviewAnswer.getId();
            //采取异步形式
            new Thread(()->{
                String jobId = HuaWeiSISUtils.doSubmit(fileUrl);
                Map<String, Object> result = HuaWeiSISUtils.getResult(jobId);
                InterviewAnswer interviewAnswerUpdate = iInterviewAnswerService.selectInterviewAnswerById(answerId);
                interviewAnswerUpdate.setContent(result.get("text").toString());
                Integer audioDuration =(Integer) result.get("audioDuration");
                interviewAnswerUpdate.setDuration(audioDuration.longValue());
                //执行修改
                iInterviewAnswerService.updateInterviewAnswer(interviewAnswerUpdate);

            }).start();



    return AjaxResult.success();

    }
}
